<?php
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );

class AuctionModel {
	function getAuctionDetails($id){
		$my_Obj = new AuctionDTO();
		$my_Obj->setKeyValue($id);
		SysDatabase::fillData($my_Obj);
		return $my_Obj;
	}

	function getCategoryDetails($id){
		$my_Obj = new CategoryDTO();
		$my_Obj->setKeyValue($id);
		SysDatabase::fillData($my_Obj);
		return $my_Obj;
	}

	function getUserDto($id){
		$my_Obj = new UserDTO();
		$my_Obj->setKeyValue($id);
		SysDatabase::fillData($my_Obj);
		return $my_Obj;
	}

	function getVehicleDto($id){
		$my_Obj = new VehicleDTO();
		$my_Obj->setKeyValue($id);
		SysDatabase::fillData($my_Obj);
		return $my_Obj;
	}

	function getBidDto($id){
		$my_Obj = new BidDTO();
		$my_Obj->setKeyValue($id);
		SysDatabase::fillData($my_Obj);
		return $my_Obj;
	}

	function getVehicleType($id){
		$sQuery	= "SELECT * FROM `category` WHERE `category_id` = '$id'";
		$rs = SysDatabase::loadObject($sQuery);
		return $rs->name;
	}

	function getCity($id){
		$sQuery	= "SELECT * FROM `city` WHERE `city_id` = '$id'";
		$rs = SysDatabase::loadObject($sQuery);
		return $rs->name;
	}

	function getState($id){
		$sQuery	= "SELECT * FROM state WHERE state_id='$id'";
		$rs = SysDatabase::loadObject($sQuery);
		return $rs->name;
	}

	function getPhotoFromGallery($id){
		$my_Obj = new PhotoDTO();
		$my_Obj->setKeyValue($id);
		SysDatabase::fillData($my_Obj);
		return $my_Obj;
	}

	function getPhotosVehicle($id, $RECORD_PER_PAGE = 12, $page = 1){
		$sQuery	= "SELECT `pg`.`photo_url` AS `url` FROM `photos_gallery` `pg`
						LEFT JOIN `vehicle_images` `vi` ON `pg`.`image_id` = `vi`.`image_id`   
						WHERE `vi`.`vehicle_id` = '$id'";		
		return SysDatabase::getSelectResult($sQuery, $RECORD_PER_PAGE, $page);
	}

	function getNameOwner($id){
		$sQuery	= "SELECT `business_name` FROM `user` WHERE `user_id` = '$id'";
		$rs = SysDatabase::loadObject($sQuery);
		return $rs->business_name;
	}

	function getMake($id){
		$sQuery	= "SELECT * FROM `maker` WHERE `maker_id` = '$id'";
		$rs = SysDatabase::loadObject($sQuery);
		return $rs->name;
	}

	function getModel($id){
		$sQuery	= "SELECT * FROM `model` WHERE `model_id` = '$id'";
		$rs = SysDatabase::loadObject($sQuery);
		return $rs->model_group;
	}

	function getModelGroupDetail($id){
		$sQuery	= "SELECT * FROM `model` WHERE `model_id` = '$id'";
		return SysDatabase::loadObject($sQuery);
	}


	function getDamage($id){
		$sQuery	= "SELECT * FROM `damage` WHERE `damage_id` = '$id'";
		$rs = SysDatabase::loadObject($sQuery);
		return $rs->damage;
	}

	function getSaleTitleType($id){
		$sQuery	= "SELECT * FROM `saletitletype` WHERE `saletitletype_id` = '$id'";
		$rs = SysDatabase::loadObject($sQuery);
		return $rs->saletitletype;
	}

	function getDriveType($id){
		$sQuery	= "SELECT * FROM `drive` WHERE `drive_id` = '$id'";
		$rs = SysDatabase::loadObject($sQuery);
		return $rs->drive;
	}

	function getFuelType($id){
		$sQuery	= "SELECT * FROM `fuel` WHERE `fuel_id` = '$id'";
		$rs = SysDatabase::loadObject($sQuery);
		return $rs->fuel;
	}


	function getOtherCurrentAuctions($owner_id, $RECORD_PER_PAGE = 5, $page = 1){
		$sQuery	= "SELECT a.auction_id, a.owner_id, a.vehicle_id, a.current_bid, a.start_date,
						 v.thumbnail, v.vehicle_name, v.year, v.maker_id, v.model_id, v.odometer, v.odometer_brand 
					FROM auctions a
					LEFT JOIN vehicle v ON a.vehicle_id = v.vehicle_id 
					WHERE a.deleted=0 AND a.owner_id='{$owner_id}' ORDER BY a.create_date DESC";	
		//					WHERE a.deleted=0 AND a.end_date>=NOW() AND a.owner_id='.$owner_id.' ORDER BY a.create_date DESC";
		return SysDatabase::getSelectResult($sQuery, $RECORD_PER_PAGE, $page);
	}
	

	function getAuctionsCate($cate_id, $RECORD_PER_PAGE = 12, $page = 1){
		$sQuery	= "SELECT a.auction_id, a.owner_id, a.vehicle_id, a.current_bid, a.start_date,
						 v.thumbnail, v.vehicle_name, v.year, v.maker_id, v.model_id, v.odometer, v.odometer_brand 
					FROM auctions a
					LEFT JOIN vehicle v ON a.vehicle_id = v.vehicle_id 
					WHERE a.deleted=0 AND v.cate_id='{$cate_id}' ORDER BY a.create_date DESC";	
		//					WHERE a.deleted=0 AND a.end_date>=NOW() AND v.cate_id='.$cate_id.' ORDER BY a.create_date DESC';
		return SysDatabase::getSelectResult($sQuery, $RECORD_PER_PAGE, $page);
	}

	function getNewestAuctions($RECORD_PER_PAGE = 12, $page = 1){
		$sQuery	= 'SELECT a.auction_id, a.owner_id, a.vehicle_id, a.current_bid, a.start_date, a.state_id, a.city_id,
						 v.thumbnail, v.vehicle_name, v.cate_id, v.damage_id, v.year, v.maker_id, v.model_id, v.odometer, v.odometer_brand  
					FROM auctions a
					LEFT JOIN vehicle v ON a.vehicle_id = v.vehicle_id 
					WHERE a.deleted=0 ORDER BY a.create_date DESC';	
		//					WHERE a.deleted=0 AND a.end_date>=NOW() ORDER BY a.create_date DESC';
		return SysDatabase::getSelectResult($sQuery, $RECORD_PER_PAGE, $page);
	}


	function checkFirstBid($user_id, $auction_id){
		$sQuery	= "SELECT COUNT(*) as `soluong` FROM `bids` WHERE `user_id` = '$user_id' AND `auction_id` = '$auction_id'";
		$rs = SysDatabase::loadObject($sQuery);
		return ($rs->soluong == 0);
	}

	

	function getCateName($id){
		$sQuery	= "SELECT * FROM `category` WHERE `category_id` = '$id'";
		$rs = SysDatabase::loadObject($sQuery);
		return $rs->name;
	}

	function getCategory(){
		$sQuery	= "SELECT * FROM `category` WHERE `deleted` = 0";
		return SysDatabase::loadObjectList($sQuery);
	}

	/**
	 *
	 * Get auctions that start in next month
	 *
	 * @param int $RECORD_PER_PAGE: Record per page number
	 * @param int $page: Current page
	 * @return SelectResult
	 */
	function getNextMonthAuctions($RECORD_PER_PAGE = 12, $page = 1) {
		$month = (int)strftime('%m');
		$year = (int)strftime('%Y');
		$nextYear = $year;
		switch ($month) {
			case 12:
				$nextMonth = 1;
				$nextYear = $year + 1;
				break;
			default:
				$nextMonth = $month + 1;
				break;
		}
		$sql = "SELECT `a`.*, `v`.`damage_id`, `v`.`cate_id`, `v`.`vehicle_name`, `v`.`thumbnail` 
				FROM `auctions` `a`
				LEFT JOIN `vehicle` `v` ON `a`.`vehicle_id` = `v`.`vehicle_id` 
				WHERE `a`.`deleted` = 0 AND `a`.`start_date` LIKE '$nextYear-$nextMonth%'";
		return SysDatabase::getSelectResult($sql, $RECORD_PER_PAGE, $page);
	}

	/**
	 *
	 * Get next month
	 * @return string: Month full name
	 */
	function getNextMonth() {
		$month = (int)strftime('%m');
		$year = (int)strftime('%Y');
		
		if($month == 12) {
			$nextMonth = 1;
			$nextYear = $year + 1;
			$date = strtotime("01-$nextMonth-$nextYear");
			return strftime('%B-%Y', $date);
		}
		else {
			$nextMonth = $month + 1;
			$date = strtotime("01-$nextMonth-$year");
			return strftime('%B', $date);
		}
	}

	/**
	 * 
	 * Check if the user is blocked from the owner or not
	 * @param int $ownerId: Auction owner ID
	 * @return number. 0 (zero) in case that the user's not blocked.
	 */
	function checkBlocked($bidder, $ownerId) {
		$sql = "SELECT COUNT(*) FROM `user_block` WHERE `owner_id` = '{$ownerId}' AND `buyer_id` = '{$bidder}'";
		$result =  SysDatabase::loadResult($sql);
//		RubikHelper::debugPrePrint($result);
		return $result;
	}
	
	function getStaticContent($title){
		$sql = "SELECT content_en FROM `static_content` WHERE `title` = '{$title}' ";
		return SysDatabase::loadResult($sql);
		
	}
	
	
	/**
	 * 
	 * Get auctions by state and city
	 * 
	 * @param int $stateId: State id
	 * @param int $cityId: City id
	 * @param int $RECORD_PER_PAGE: Number of records per page
	 * @param int $page: Page number
	 * @return SelectResult
	 */
	function getAuctionsByStateCity($stateId, $cityId = null, $RECORD_PER_PAGE = 12, $page = 1) {
		$sql = "SELECT `a`.*, `v`.`damage_id`, `v`.`cate_id`, `v`.`vehicle_name`, `v`.`thumbnail` 
				FROM `auctions` `a`
				LEFT JOIN `vehicle` `v` ON `a`.`vehicle_id` = `v`.`vehicle_id` 
				WHERE `a`.`deleted` = 0 AND `a`.`state_id` = '$stateId'";
		if($cityId) {
			$sql .= " AND `a`.`city_id` = '$cityId'";
		}
		return SysDatabase::getSelectResult($sql, $RECORD_PER_PAGE, $page);
	}
}
?>